package com.steven.team.dao

import com.steven.team.pojo.ChatMessage
import com.steven.team.pojo.MessageList
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository
interface ChatDao {

    @Insert("INSERT INTO chat_message(sendUser,receiveUser,postMessage,readStatus,createTime) " +
            "VALUE(#{chatMessage.sendUser},#{chatMessage.receiveUser},#{chatMessage.postMessage},false,#{chatMessage.createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "chatMessage.id", keyColumn = "id")
    fun addMessage(@Param("chatMessage") chatMessage: ChatMessage): Long

    @Select("select resultB.*, iconUrl receiveUserIcon\n" +
            "from (select resultA.*, iconUrl sendUserIconUrl,uname sendUserName\n" +
            "      from (\n" +
            "               select *\n" +
            "               from chat_message\n" +
            "               where ((sendUser = #{uid} and receiveUser = #{friendId})\n" +
            "                  or (sendUser = #{friendId} and receiveUser = #{uid}))\n" +
            "                   and createTime < #{lastCreateTime}\n" +
            "           ) resultA\n" +
            "               LEFT JOIN user_detail ON resultA.sendUser = user_detail.uid) resultB\n" +
            "         LEFT JOIN user_detail ON resultB.receiveUser = user_detail.uid\n" +
            "order by createTime desc limit 12")
    fun queryAllBy2User(uid: Long, friendId: Long, lastCreateTime: Long): List<ChatMessage>

    @Update("UPDATE chat_message SET readStatus=true " +
            "where sendUser=#{sendUser} and receiveUser=#{receiveUser}")
    fun updateMessageStatusBy2Users(sendUser: Long, receiveUser: Long): Int

    @Select("SELECT\n" +
            "\tid,\n" +
            "\tsendUser,\n" +
            "\treceiveUser,\n" +
            "\tpostMessage,\n" +
            "\treadStatus,\n" +
            "\tcreateTime,\n" +
            "\tuname sendUserName,\n" +
            "\ticonUrl sendUserIconUrl\n" +
            "FROM\n" +
            "\tchat_message\n" +
            "\tLEFT JOIN user_detail ON sendUser = user_detail.uid \n" +
            "WHERE\n" +
            "\treceiveUser = #{uid} and readStatus = 0\n" +
            "ORDER BY\n" +
            "\tcreateTime")
    fun queryAllUnreadMessage(uid: Long): List<ChatMessage>


    @Select("SELECT\n" +
            "    a.sendUser,\n" +
            "    a.receiveUser,\n" +
            "    uname sendUserName,\n" +
            "    iconUrl sendUserIconUrl,\n" +
            "    count( a.sendUser ) msgNum\n" +
            "    FROM\n" +
            "    ( SELECT sendUser, receiveUser FROM chat_message WHERE receiveUser = 10 ) a\n" +
            "    LEFT JOIN user_detail ON a.sendUser = user_detail.uid\n" +
            "    GROUP BY\n" +
            "    a.sendUser")
    fun getMessageList(uid: Long): List<MessageList>


    @Select("SELECT\n" +
            "\tid,\n" +
            "\tsendUser,\n" +
            "\treceiveUser,\n" +
            "\tpostMessage,\n" +
            "\treadStatus,\n" +
            "\tcreateTime,\n" +
            "\tuname sendUserName,\n" +
            "\ticonUrl sendUserIconUrl\n" +
            "FROM\n" +
            "\tchat_message\n" +
            "\tLEFT JOIN user_detail ON sendUser = user_detail.uid \n" +
            "WHERE\n" +
            "\tchat_message.id = #{messageId} \n" +
            "ORDER BY\n" +
            "\tcreateTime")
    fun queryMessageById(messageId: Long): List<ChatMessage>
}